package cn.edu.cjw.APMFP.user.Service;

import java.util.ArrayList;

import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.user.DAO.UserDAO;
import cn.edu.cjw.APMFP.user.DAO.UserDAOImpl;
import cn.edu.cjw.APMFP.user.pojo.User;

public class UserServiceImpl implements UserService {

	UserDAO uDao = new UserDAOImpl();

	public ArrayList<User> showAll() throws Exception {

		return uDao.showAll();
	}

	public boolean addUser(User user) throws Exception {

		boolean flag = false;

		User searchUserByAccount = uDao.SearchUserByAccount(user.getAccount());

		if (searchUserByAccount.judge()) {
			new Exception("员工已存在或工号重复！").printStackTrace();
			return flag;
		} else {
			flag = uDao.AddAndUpdateUser(user);
		}

		return flag;
	}

	public boolean removeUSer(User user) throws Exception {

		return uDao.RemoveUser(user);

	}

	public boolean updateUserPassword(User user, String newPasswd) throws Exception {

		user.setPassWord(newPasswd);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUserName(User user, String newName) throws Exception {
		user.setName(newName);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUserRole(User user, RoleRight newRole) throws Exception {
		user.setuRole(newRole);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUserGender(User user, String newGender) throws Exception {
		user.setGender(newGender);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUserAge(User user, int newAge) throws Exception {
		user.setAge(newAge);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUserIsdeactivate(User user, boolean newDtive) throws Exception {
		user.setDeactivate(newDtive);

		return uDao.AddAndUpdateUser(user);
	}

	public boolean updateUser(User user, String newPasswd, String newName, RoleRight newRole, String newGender,
                              int newAge, boolean newDtive) throws Exception {
		user.setPassWord(newPasswd);
		user.setName(newName);
		user.setuRole(newRole);
		user.setGender(newGender);
		user.setAge(newAge);
		user.setDeactivate(newDtive);
		return uDao.AddAndUpdateUser(user);
	}

	public User SearchUserByAccount(String account) throws Exception {

		return uDao.SearchUserByAccount(account);
	}

	public ArrayList<User> SearchUserByName(String name) throws Exception {

		return uDao.SearchUserByName(name);
	}

	public ArrayList<User> SearchUserByRole(RoleRight right) throws Exception {

		return uDao.SearchUsersByRole(right.getRNum());
	}

}
